package com.liuz.wechat.controller;

import com.liuz.wechat.bean.About;
import com.liuz.wechat.bean.JieXiResult;
import com.liuz.wechat.bean.JieXiType;
import com.liuz.wechat.dao.JiexiForSql;
import com.liuz.wechat.server.AboutService;
import com.liuz.wechat.server.impl.w0ai1uoApiServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

@Slf4j
@RestController
@RequestMapping("/wechat")
@CrossOrigin
public class WeChatController {
    @Autowired
    private AboutService aboutService;
    @Autowired
    private JiexiForSql jiexiforsql;
//    @Autowired
//    private ShiPinJieXi shipinjiexi;
    /**
     * 测试
     * @param test
     * @return
     */
    @GetMapping("user")
    public String parse(String test){
        return "nihao";
    }

    /**
     * 下载文件
     * @param downurl
     * @param request
     * @param response
     */
    @GetMapping("/down")//
    public void download(String downurl, HttpServletRequest request, HttpServletResponse response) throws IOException {
//        String path=request.getSession().getServletContext().getRealPath("/")+ "upload\\";

        //根据url获取输入流
        URL url = new URL(downurl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        //设置超时间为3秒
        conn.setConnectTimeout(3 * 1000);
        //防止屏蔽程序抓取而返回403错误
        conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

        //得到输入流
        InputStream inputStream = conn.getInputStream();
        //-------------------------------------------
        OutputStream outputStream = response.getOutputStream();
//        response.setContentType("application/x-download");
        response.setContentType("video/mp4");
//        response.addHeader("Content-Disposition", "attachment;filename=" + "test.mp4");
        IOUtils.copy(inputStream, outputStream);
        //下载
//        try (InputStream inputStream = new FileInputStream(new File(path, id ));
//             OutputStream outputStream = response.getOutputStream();) {
//
//            response.setContentType("application/x-download");
//            response.addHeader("Content-Disposition", "attachment;filename=" + id);
//
//            IOUtils.copy(inputStream, outputStream);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }

    /**
     * 查询解析页面展示信息
     * @return
     */
    @GetMapping("/getAbout")
    public About getAbout (){
//        System.out.println("开始查询...........");
        log.info("开始查询关于信息.......");
        return aboutService.findAbout();
    }
    @GetMapping("/jieXi")
    public JieXiResult jieXi(@RequestParam(value = "url", required = true) String url) throws IllegalAccessException, InstantiationException {
        log.info("url:"+url);
        log.info("当前url:"+url);
        JieXiType resultType=jiexiforsql.findJieXiType();
        log.info("resultType:"+resultType);
        String JieXi_Type=resultType.getType();
        log.info("type:"+JieXi_Type);
        if("Allshipin".equals(JieXi_Type)){
            return w0ai1uoApiServiceImpl.class.newInstance().freeJiexi(url);
        }else if ("tecmsApi".equals(JieXi_Type)){
            return new JieXiResult();
        }
        log.info("找不到对应解析服务方");
        return new JieXiResult();

    }



}
